<?php
/**
 * Created by PhpStorm.
 * Volunteer: yellow
 * Date: 2019/5/15
 * Time: 15:44
 */

namespace app\admin\controller;
use PHPExcel;
use PHPExcel_IOFactory;
use app\common\model\Site;
use app\common\model\SiteTeam;
use app\common\model\ServiceType;
use app\common\model\Volunteer as VolunteerModel;
use app\admin\logic\VolunteersLogic;
use think\facade\Request;
use app\vehicle\logic\TplLogic;
class Volunteer extends Base {

    private $VolunteerLogic;

    public function __construct()
    {
        parent::__construct();
        $this->VolunteerLogic = new VolunteersLogic();
    }
    public function batch(){
        $where = [];
        $site_id = $_SESSION['xm_travel']['admin_info']['site_id'];
        $site_id && $where[] = ['id','=',$site_id];
        $this->assign('site',Site::where($where)->select());

        $where = [];
        $oid = $_SESSION['xm_travel']['admin_info']['oid'];
        $oid && $where[] = ['id','=',$site_id];
        $this->assign('site_team',SiteTeam::where($where)->select());

        return $this->fetch();
    }
    public function exports(){
        $objPHPExcel=new \PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);
// 设置列的宽度
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(50);
// 设置表头
        $objPHPExcel->getActiveSheet()->SetCellValue('A1', '序号');
        $objPHPExcel->getActiveSheet()->SetCellValue('B1', '昵称');
        $where = Request::param();
        if($where['type']=='all'){
            $where['limit'] = 100000;
        }
        $res = $this->VolunteerLogic->VolunteerList($where, $where['page']);
        $data = $res['data'];
        $num = 2;
        foreach ($data as $k => $v) {
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $num, $v['nums']);

            $objDrawing[$k] = new \PHPExcel_Worksheet_Drawing();
            $objDrawing[$k]->setPath('.'.$v['thumb']);//这里拼接 . 是因为要在根目录下获取
            // 设置宽度高度
            $objDrawing[$k]->setHeight(250);//照片高度
            $objDrawing[$k]->setWidth(250); //照片宽度
            /*设置图片要插入的单元格*/
            $objDrawing[$k]->setCoordinates('B'.$num);
            // 图片偏移距离
            $objDrawing[$k]->setOffsetX(12);
            $objDrawing[$k]->setOffsetY(12);
            $objDrawing[$k]->setWorksheet($objPHPExcel->getActiveSheet());

            $objPHPExcel->getActiveSheet()->getRowDimension($num)->setRowHeight(210);
            $num++;
        }
        $fileName = "二维码" . date('Y-m-d', time()) . rand(1, 1000);
        $xlsName = iconv('utf-8', 'gb2312', $fileName);
        $objPHPExcel->getActiveSheet()->setTitle('sheet'); // 设置工作表名
        $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel); //下载 excel5与excel2007
        ob_end_clean(); // 清除缓冲区,避免乱码
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl;charset=UTF-8");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header("Content-Disposition:attachment;filename=" . $xlsName . ".xls");
        header("Content-Transfer-Encoding:binary");
        $objWriter->save("php://output");
    }
    public function export(){
        $objPHPExcel=new \PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);
// 设置列的宽度
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
// 设置表头
        $objPHPExcel->getActiveSheet()->SetCellValue('A1', '序号');
        $objPHPExcel->getActiveSheet()->SetCellValue('B1', '昵称');
        $objPHPExcel->getActiveSheet()->SetCellValue('C1', '姓名');
        $objPHPExcel->getActiveSheet()->SetCellValue('D1', '站点');
        $objPHPExcel->getActiveSheet()->SetCellValue('E1', '服务队');
        $objPHPExcel->getActiveSheet()->SetCellValue('F1', '身份证号');
        $objPHPExcel->getActiveSheet()->SetCellValue('G1', '服务类型');
        $objPHPExcel->getActiveSheet()->SetCellValue('H1', '是否党员');
        $objPHPExcel->getActiveSheet()->SetCellValue('I1', '服务时长');
        $objPHPExcel->getActiveSheet()->SetCellValue('J1', '活动时长');
        $objPHPExcel->getActiveSheet()->SetCellValue('K1', '状态');
        $objPHPExcel->getActiveSheet()->SetCellValue('L1', '组织状态');
        $objPHPExcel->getActiveSheet()->SetCellValue('M1', '是否发布');
        $objPHPExcel->getActiveSheet()->SetCellValue('N1', '注册时间');

        $where = Request::param();
        if($where['type']=='all'){
            $where['limit'] = 100000;
        }
        $res = $this->VolunteerLogic->VolunteerList($where, $where['page']);
        $data = $res['data'];
        $num = 2;
        foreach ($data as $k => $v) {
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $num, ($k+1));
            $objPHPExcel->getActiveSheet()->SetCellValue('B' . $num, $v['nickname']);
            $objPHPExcel->getActiveSheet()->SetCellValue('C' . $num, $v['realname']);
            $objPHPExcel->getActiveSheet()->SetCellValue('D' . $num, $v['site']);
            $objPHPExcel->getActiveSheet()->SetCellValue('E' . $num, $v['team']);
            $objPHPExcel->getActiveSheet()->SetCellValue('F' . $num, ' '.$v['id_card']);
            $objPHPExcel->getActiveSheet()->SetCellValue('G' . $num, $v['skill']);
            $objPHPExcel->getActiveSheet()->SetCellValue('H' . $num, $v['is_party']?'是':'否');
            $objPHPExcel->getActiveSheet()->SetCellValue('I' . $num, $v['duration']);
            $objPHPExcel->getActiveSheet()->SetCellValue('J' . $num, $v['duration1']);
            $objPHPExcel->getActiveSheet()->SetCellValue('K' . $num, $v['status']);
            if($v['o_status']==1){
                $o_status='审核通过';
            }else if($v['o_status']==2){
                $o_status='审核不通过';
            }else{
                $o_status='审核中';
            }
            $objPHPExcel->getActiveSheet()->SetCellValue('L' . $num, $o_status);
            $objPHPExcel->getActiveSheet()->SetCellValue('M' . $num, $v['is_add']?'是':'否');
            $objPHPExcel->getActiveSheet()->SetCellValue('N' . $num, $v['create_time']);
            $num++;
        }
        $fileName = "志愿者表" . date('Y-m-d', time()) . rand(1, 1000);
        $xlsName = iconv('utf-8', 'gb2312', $fileName);
        $objPHPExcel->getActiveSheet()->setTitle('sheet'); // 设置工作表名
        $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel); //下载 excel5与excel2007
        ob_end_clean(); // 清除缓冲区,避免乱码
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl;charset=UTF-8");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header("Content-Disposition:attachment;filename=" . $xlsName . ".xls");
        header("Content-Transfer-Encoding:binary");
        $objWriter->save("php://output");
    }

    public function audit_u(){
        $this->assign('status',0);
        return $this->fetch('index');
    }
    public function audit_o(){
        $this->assign('o_status',0);
        return $this->fetch('index');
    }

    public function index(){
        $this->assign('site',Site::where('cate_id','eq','2')->select());
        $this->assign('site_list',Site::where('cate_id','eq','1')->select());

        $oid = $_SESSION['xm_travel']['admin_info']['oid'];
        $site_id = $_SESSION['xm_travel']['admin_info']['site_id'];
        if($site_id){
            $this->assign('site_team', SiteTeam::where('sid','=',$site_id)->select());
        }else if($oid){
            $this->assign('site_team', SiteTeam::where('oid','=',$oid)->select());
        }else {
            $this->assign('site_team', SiteTeam::select());
        }
        if(input('check')){
            TplLogic::examineTpls('oE4HI5YIX1qSfK2N4z_d8OCgTy5w','test','test');
        }
        return $this->fetch();
    }

    public function read(){
        $this->assign('id',input('id'));
        return $this->fetch();
    }

    public function audit(){
        return $this->fetch();
    }

    public function ajaxRead(){
        $data = Request::param();
        $VolunteerList = $this->VolunteerLogic->VolunteerIntegral($data, self::$nowPage);
        return json($VolunteerList);
    }

    public function ajaxIndex(){
        $data = Request::param();
        $data['is_after'] = 0;
        $VolunteerList = $this->VolunteerLogic->VolunteerList($data, self::$nowPage);
        return json($VolunteerList);
    }

    public function ajaxAudit(){
        $data = Request::param();
        $VolunteerList = $this->VolunteerLogic->auditList($data, self::$nowPage);
        return json($VolunteerList);
    }

    public function add(){
        $id = Request::get('id');
        $act = "add";
        if ($id){
            $act = "edit";
            $info = $this->VolunteerLogic->VolunteerInfo($id);
            $this->assign('info', $info['data']);
        }
        
        $organization = SiteTeam::where('status','eq',1)->select();
        $this->assign('organization', $organization);
        $site = Site::select();
        $this->assign('site', $site);

        $service = ServiceType::select();
        $this->assign('service', $service);
        $this->assign('act', $act);
        return view();
    }

    public function handle(){
        $data = Request::request();
        $res = $this->VolunteerLogic->handle($data);
        return json($res);
    }


    public function audit_info(){
        $id = Request::get('id');
        $info = $this->VolunteerLogic->auditInfo($id);
        $this->assign('info', $info['data']);
        return view();
    }


    public function auditHandle(){
        $data = Request::request();
        $res = $this->VolunteerLogic->auditHandle($data);
        return json($res);
    }
}